package cn.zebra.dev.admin.module.admin.controller;

import cn.zebra.dev.admin.module.admin.dto.MenuDTO;
import cn.zebra.dev.admin.module.login.SecurityAdminService;
import cn.zebra.dev.admin.module.menu.constant.MenuEnum;
import cn.zebra.dev.admin.module.menu.entity.AdminMenuEntity;
import cn.zebra.dev.admin.module.menu.service.AdminMenuService;
import cn.zebra.dev.common.restful.Result;
import cn.zebra.dev.common.utils.StrUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.LinkedList;
import java.util.List;

/**
 * @author runnable@sina.cn
 */
@Api(tags = "菜单服务")
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private AdminMenuService menuService;

    @Autowired
    private SecurityAdminService securityService;

    @GetMapping("/nav")
    @ApiOperation("导航菜单")
    public Result<List<AdminMenuEntity>> nav() {
        Long adminId = securityService.getId();
        return Result.success(menuService.findAdminUserMenuList(adminId));
    }

    @GetMapping("/get/{id}")
    @ApiOperation("菜单信息")
    public Result<MenuDTO> get(@PathVariable Long id) {
        AdminMenuEntity menu = menuService.findById(id);
        MenuDTO dto = new MenuDTO();
        BeanUtils.copyProperties(menu, dto);
        return Result.success(dto);
    }

    @GetMapping("/list")
    @ApiOperation("导航菜单")
    public Result<List<MenuDTO>> list() {
        List<AdminMenuEntity> list = menuService.queryList();
        List<MenuDTO> dtoList = new LinkedList<>();
        list.forEach(entity -> {
            MenuDTO dto = new MenuDTO();
            BeanUtils.copyProperties(entity, dto);
            dtoList.add(dto);
        });
        return Result.success(dtoList);
    }

    @GetMapping("/select")
    @ApiOperation("选择菜单(添加、修改菜单)")
    public Result<List<AdminMenuEntity>> select() {
        //查询列表数据
        List<AdminMenuEntity> menuList = menuService.findNotButtonList();
        //添加顶级菜单
        AdminMenuEntity root = new AdminMenuEntity();
        root.setId(0L);
        root.setMenuName("一级菜单");
        root.setParentId(-1L);
        root.setOpen(true);
        menuList.add(root);
        return Result.success(menuList);
    }

    @PostMapping("/save")
    @ApiOperation("保存菜单")
    public Result save(@Valid @RequestBody AdminMenuEntity entity) {
        String errorMsg = verifyForm(entity);
        if (StrUtils.isNotBlank(errorMsg)) {
            return Result.error(errorMsg);
        }
        if (menuService.saveMenu(entity, securityService.getId())) {
            return Result.success("添加成功!");
        }
        return Result.error("保存失败!");
    }

    @PostMapping("/update/{id}")
    @ApiOperation("修改菜单")
    public Result update(@PathVariable Long id, @Valid @RequestBody AdminMenuEntity entity) {
        String errorMsg = verifyForm(entity);
        if (StrUtils.isNotBlank(errorMsg)) {
            return Result.error(errorMsg);
        }
        if (menuService.updateMenu(id, entity, securityService.getId())) {
            return Result.success("修改成功!");
        }
        return Result.error("修改失败!");
    }

    @GetMapping("/delete/{id}")
    @ApiOperation("删除菜单")
    public Result delete(@PathVariable Long id) {
        //判断是否有子菜单或按钮
        List<AdminMenuEntity> menuList = menuService.queryListParentId(id);
        if (menuList.size() > 0) {
            return Result.error("请先删除子菜单或按钮");
        }
        if (menuService.deleteMenu(id)) {
            return Result.success("删除成功!");
        }
        return Result.error("删除失败!");
    }

    private String verifyForm(AdminMenuEntity menu) {

        //菜单
        Integer menuType = menu.getMenuType();
        if (menuType == MenuEnum.MENU.getValue()) {
            if (StrUtils.isBlank(menu.getUrl())) {
                return "菜单URL不能为空";
            }
        }

        //上级菜单类型
        int parentType = MenuEnum.CATALOG.getValue();
        if (menu.getParentId() != 0) {
            AdminMenuEntity parentMenu = menuService.getById(menu.getParentId());
            parentType = parentMenu.getMenuType();
        }

        //目录、菜单
        if (menuType == MenuEnum.CATALOG.getValue() || menuType == MenuEnum.MENU.getValue()) {
            if (parentType != MenuEnum.CATALOG.getValue()) {
                return "上级菜单只能为目录类型";
            }
            return null;
        }

        //按钮
        if (menuType == MenuEnum.BUTTON.getValue()) {
            if (parentType != MenuEnum.MENU.getValue()) {
                return "上级菜单只能为菜单类型";
            }
        }
        return null;
    }
}
